package com.leopardcat.firewall;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView;

public class SMSBlackListActivity extends Activity
	implements AdapterView.OnItemClickListener {
	
	static final int			INPUT_NUMBER_DIALOG = 0;
	
	private SMSBlackListDB		mSMSBlackListDB;
	private ListView			mListView;
	private Cursor				mCursor;
	private String				mNumberText = "";
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		mSMSBlackListDB.close();
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// create
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.sms_black_list);
		
		setListView();
	}
	

	private void setListView() {
		mSMSBlackListDB = new SMSBlackListDB(this);

		mCursor = mSMSBlackListDB.select();
		
		mListView = (ListView) findViewById(R.id.listView1);
		mListView.setAdapter( new SMSBlackListAdapter(this, mSMSBlackListDB));
		mListView.setOnItemClickListener(this);
		
		Button btn = (Button) findViewById(R.id.add_btn);
		btn.setOnClickListener(btnClickHandler);
		
		btn = (Button) findViewById(R.id.delete_btn);
		btn.setOnClickListener(btnClickHandler);
	}
	
	private OnClickListener btnClickHandler = new OnClickListener() {
		public void onClick(View v) {
			if( v.getId() == R.id.add_btn ) {
				LayoutInflater inflater = LayoutInflater.from(SMSBlackListActivity.this);
				final View mDialogView = inflater.inflate(R.layout.input_text_dialog, null);

				AlertDialog.Builder builder = new AlertDialog.Builder(SMSBlackListActivity.this);
				builder.setView(mDialogView);
				builder.setTitle("Input number").setPositiveButton("ok", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// add number to blacklist
						EditText t = (EditText) mDialogView.findViewById(R.id.number_text);

						SMSBlackListAdapter adapter = (SMSBlackListAdapter)mListView.getAdapter();
						adapter.insertNumber(t.getText().toString());
						
					}
				}).show();
				
			}
			if( v.getId() == R.id.delete_btn ) {
				// remove number from list
				SMSBlackListAdapter adapter = (SMSBlackListAdapter)mListView.getAdapter();
				adapter.delete(mNumberText);
				
			}
		}
		
	};
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		// TODO Auto-generated method stub
		TextView textview = (TextView) view;
		mNumberText = textview.getText().toString();

		
	}

	
	// black list adapter
	public class SMSBlackListAdapter extends BaseAdapter {
		private Context			mContext;
		private Cursor			mCursor;
		private SMSBlackListDB	mDb;
		
		public SMSBlackListAdapter(Context context, SMSBlackListDB db) {
			mContext = context;
			mCursor  = db.select();
			mDb = db;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mCursor.getCount();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView mTextView;
			if ( convertView == null ) {
				mTextView = new TextView(mContext);
			}
			else {
				mTextView = (TextView) convertView;
			}
			
			mTextView.setTextSize(32);
			mCursor.moveToPosition(position);
			
			mTextView.setText(mCursor.getString(1));
			
			return mTextView;
		}
		
		public void insertNumber(String number) {
			mDb.insert(number);
			mCursor.requery();
			notifyDataSetChanged();
		}
		
		public void delete(String number) {
			mDb.delete(number);
			mCursor.requery();
			notifyDataSetChanged();
			
		}
		
		public void delete(int id) {
			mDb.delete(id);
			mCursor.requery();
			notifyDataSetChanged();
		}
		
	}

}
